package com.smile.service.client;

import com.alibaba.fastjson2.JSONObject;
import com.smile.service.entity.config.DeviceAppConfig;
import com.smile.service.entity.config.DeviceConfigInfo;
import com.smile.service.entity.organization.*;
import com.smile.service.entity.response.DeviceConfigResponse;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Component
public class DeviceClient {

//    @Autowired
//    private MqttClient mqttClient;

    public void getConfig() throws MqttException {
        DeviceConfigInfo deviceConfigInfo = new DeviceConfigInfo();
        deviceConfigInfo.setConfigScope("product");
        deviceConfigInfo.setConfigType("json");

        DeviceConfigResponse<DeviceConfigInfo> deviceConfigResponse = new DeviceConfigResponse<>();
        deviceConfigResponse.setBid(UUID.randomUUID().toString());
        deviceConfigResponse.setTimestamp(System.currentTimeMillis());
        deviceConfigResponse.setTid(UUID.randomUUID().toString());
        deviceConfigResponse.setData(deviceConfigInfo);
        deviceConfigResponse.setMethod("config");
        deviceConfigResponse.setGateway("8UUXN2800A00HX");

        MqttMessage message = new MqttMessage();
        message.setQos(1);
        message.setRetained(true);
        message.setPayload(JSONObject.toJSONString(deviceConfigResponse).getBytes());
        MqttClient mqttClient = new MqttClient("tcp://120.211.119.131:1883", "feisuan-mqtt-producer-client", new MemoryPersistence());
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setUserName("feisuan");
        connOpts.setPassword("feisuan123456".toCharArray());
        mqttClient.connect(connOpts);

        mqttClient.publish("thing/product/8UUXN2800A00HX/requests", message);

    }

    public void configAppDevice() throws MqttException {


//        AirportBindErrorResult airportBindErrorResult = new AirportBindErrorResult();
//        airportBindErrorResult.setResultCode(0);
//        AirportBindErrorResult.BindErrorOutput bindErrorOutput = new AirportBindErrorResult.BindErrorOutput();
//        bindErrorOutput.setErrorInfos(new ArrayList<>());
//        airportBindErrorResult.setOutput(bindErrorOutput);
        DeviceConfigResponse<DeviceAppConfig> deviceConfigResponse = new DeviceConfigResponse<>();


        DeviceAppConfig deviceAppConfig = new DeviceAppConfig();
        deviceAppConfig.setAppId("144844");
        deviceAppConfig.setAppKey("db858df7ef2c2de9b566414a0d54de9");
        deviceAppConfig.setAppLicense("BVTjqXIO2t8OlaIpK2XEKohfC0eBIQx2SIM6o0qUS6wYe9mYG3M163Gq1DeJzrbvlSKZ/sFKlr7eTO6u9OFKBkZfpbJQy/yrFDNaIKwM+hSS1OFjCLibdi2jxjlnDpW+JnHl0SplUOFbErCOTVYYAckEuMXf7wqHQSofSYrmgpY=");

        deviceConfigResponse.setData(deviceAppConfig);

        //{"tid":"b098d85b-b9a3-4a89-b4c6-afb392bd4a27","bid":"4f762225-b612-4395-9cbc-1b9cb4637cbb","timestamp":1750744078601,"method":"config","data":{"config_type":"json","config_scope":"product"},"gateway":"8UUXN2800A00HX"}

        deviceConfigResponse.setBid("4f762225-b612-4395-9cbc-1b9cb4637cbb");
        deviceConfigResponse.setTid("b098d85b-b9a3-4a89-b4c6-afb392bd4a27");
        deviceConfigResponse.setGateway("1581F8HGX252500A0022");
        deviceConfigResponse.setTimestamp(System.currentTimeMillis());
        deviceConfigResponse.setMethod("config");


        MqttMessage message = new MqttMessage();
        message.setQos(1);
        message.setRetained(true);
        message.setPayload(JSONObject.toJSONString(deviceConfigResponse).getBytes());
        MqttClient mqttClient = new MqttClient("tcp://120.211.119.131:1883", "feisuan-mqtt-producer-client", new MemoryPersistence());
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setUserName("feisuan");
        connOpts.setPassword("feisuan123456".toCharArray());
        mqttClient.connect(connOpts);

        mqttClient.publish("thing/product/8UUXN2800A00HX/requests_reply", message);

    }

    //{"deviceType":"Drone","device_binding_code":"iOfc2X","device_callsign":"飞行员一号","device_model_key":"0-100-1","organization_id":"1","sn":"8UUXN2800A00HX"}
    public void getDroneStatus() throws MqttException {
        AirportBindStatusData airportBindStatusData = new AirportBindStatusData();
        AirportBindStatusData.BoundDevice boundDevice = new AirportBindStatusData.BoundDevice();
        boundDevice.setSerialNumber("1581F8HGX252500A0022");
        List<AirportBindStatusData.BoundDevice> devices = new ArrayList<>();
        devices.add(boundDevice);
        airportBindStatusData.setDevices(devices);

        DeviceConfigResponse<AirportBindStatusData> deviceConfigResponse = new DeviceConfigResponse<>();
        deviceConfigResponse.setBid(UUID.randomUUID().toString());
        deviceConfigResponse.setTid(UUID.randomUUID().toString());

        deviceConfigResponse.setData(airportBindStatusData);
        deviceConfigResponse.setTimestamp(System.currentTimeMillis());
        deviceConfigResponse.setMethod("airport_bind_status");
//        deviceConfigResponse.setGateway("8UUXN2800A00HX");

        MqttMessage message = new MqttMessage();
        message.setQos(1);
        message.setRetained(true);
        message.setPayload(JSONObject.toJSONString(deviceConfigResponse).getBytes());
        MqttClient mqttClient = new MqttClient("tcp://120.211.119.131:1883", "feisuan-mqtt-producer-client", new MemoryPersistence());
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setUserName("feisuan");
        connOpts.setPassword("feisuan123456".toCharArray());
        mqttClient.connect(connOpts);

        mqttClient.publish("thing/product/" + "8UUXN2800A00HX" + "/requests", message);
//        mqttClient.publish("thing/product/" + "1581F8HGX252500A0022" + "/requests", message);

    }

    //{"deviceType":"Drone","device_binding_code":"iOfc2X","device_callsign":"飞行员一号","device_model_key":"0-100-1","organization_id":"1","sn":"8UUXN2800A00HX"}
    public void setAirportOrganizationInfo() throws MqttException {
        AirportOrganizationInfo airportOrganizationInfo = new AirportOrganizationInfo();
        airportOrganizationInfo.setOrganizationId("9veeb2");
        airportOrganizationInfo.setDeviceBindingCode("iOfc2X");
        DeviceConfigResponse<AirportOrganizationInfo> deviceConfigResponse = new DeviceConfigResponse<>();

        deviceConfigResponse.setBid("d29a580d-298f-44ce-8b5f-32c5f0b69528");
        deviceConfigResponse.setTid("044bc712-739a-4f9f-bc39-967fb4f0229f");
        deviceConfigResponse.setData(airportOrganizationInfo);

        deviceConfigResponse.setTimestamp(System.currentTimeMillis());
        deviceConfigResponse.setMethod("airport_organization_get");
        deviceConfigResponse.setGateway("8UUXN2800A00HX");


        MqttMessage message = new MqttMessage();
        message.setQos(1);
        message.setRetained(true);
        message.setPayload(JSONObject.toJSONString(deviceConfigResponse).getBytes());
        MqttClient mqttClient = new MqttClient("tcp://120.211.119.131:1883", "feisuan-mqtt-producer-client", new MemoryPersistence());
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setUserName("feisuan");
        connOpts.setPassword("feisuan123456".toCharArray());
        mqttClient.connect(connOpts);

        mqttClient.publish("thing/product/" + "8UUXN2800A00HX" + "/requests", message);

    }

    public void bind1() throws MqttException {
        //{"bid":"d29a580d-298f-44ce-8b5f-32c5f0b69528","data":{"deviceSNs":["1581F8HGX252500A0022"],"devices":[{"sn":"1581F8HGX252500A0022"}]},"gateway":"8UUXN2800A00HX","method":"airport_organization_bind","tid":"044bc712-739a-4f9f-bc39-967fb4f0229f","timestamp":1750741576955}

        AirportBindStatusDetailedData.DeviceBindStatus deviceBindStatus = new AirportBindStatusDetailedData.DeviceBindStatus();
        deviceBindStatus.setSerialNumber("1581F8HGX252500A0022");
        deviceBindStatus.setDeviceBoundToOrganization(true);
        deviceBindStatus.setOrganizationId("9veeb2");
        deviceBindStatus.setOrganizationName("feisuan");
        deviceBindStatus.setDeviceCallsign("飞行员一号");
        AirportBindStatusDetailedData airportBindStatusDetailedData = new AirportBindStatusDetailedData();
        List<AirportBindStatusDetailedData.DeviceBindStatus> devices = new ArrayList<>();
        devices.add(deviceBindStatus);
        AirportBindStatusDetailedData.BindStatusOutput bindStatusOutput = new AirportBindStatusDetailedData.BindStatusOutput();
        bindStatusOutput.setBindStatusList(devices);

        airportBindStatusDetailedData.setOutput(bindStatusOutput);
        DeviceConfigResponse<AirportBindStatusDetailedData> deviceConfigResponse = new DeviceConfigResponse<>();
        deviceConfigResponse.setBid("d29a580d-298f-44ce-8b5f-32c5f0b69528");
        deviceConfigResponse.setTid("044bc712-739a-4f9f-bc39-967fb4f0229f");
        deviceConfigResponse.setData(airportBindStatusDetailedData);

        deviceConfigResponse.setTimestamp(System.currentTimeMillis());
        deviceConfigResponse.setMethod("airport_bind_status");
        deviceConfigResponse.setGateway("8UUXN2800A00HX");


        MqttMessage message = new MqttMessage();
        message.setQos(1);
        message.setRetained(true);
        message.setPayload(JSONObject.toJSONString(deviceConfigResponse).getBytes());
        MqttClient mqttClient = new MqttClient("tcp://120.211.119.131:1883", "feisuan-mqtt-producer-client", new MemoryPersistence());
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setUserName("feisuan");
        connOpts.setPassword("feisuan123456".toCharArray());
        mqttClient.connect(connOpts);

        mqttClient.publish("thing/product/" + "8UUXN2800A00HX" + "/requests_reply", message);

    }

    public void testBind() throws MqttException {

        DeviceConfigResponse<AirportBindErrorResult> deviceConfigResponse = new DeviceConfigResponse<>();
        deviceConfigResponse.setBid("393ad55c-c835-4770-b6fd-ce36ab79c34b");
        deviceConfigResponse.setTid("5fcb69d1-00d9-44da-b33e-ef13d0b74759");

        AirportBindErrorResult airportBindErrorResult = new AirportBindErrorResult();
        airportBindErrorResult.setResultCode(666);
        AirportBindErrorResult.BindErrorOutput bindErrorOutput = new AirportBindErrorResult.BindErrorOutput();

        AirportBindErrorResult.ErrorInfo errorInfo = new AirportBindErrorResult.ErrorInfo();
        errorInfo.setErrorCode(210231);
        errorInfo.setSerialNumber("1581F8HGX252500A0022");

        AirportBindErrorResult.ErrorInfo errorInfo1 = new AirportBindErrorResult.ErrorInfo();
        errorInfo1.setErrorCode(210231);
        errorInfo1.setSerialNumber("8UUXN2800A00HX");

        List<AirportBindErrorResult.ErrorInfo> errorInfos = new ArrayList<>();
        errorInfos.add(errorInfo);
        errorInfos.add(errorInfo1);

        bindErrorOutput.setErrorInfos(errorInfos);
        airportBindErrorResult.setOutput(bindErrorOutput);
        deviceConfigResponse.setData(airportBindErrorResult);
        deviceConfigResponse.setTimestamp(System.currentTimeMillis());
        deviceConfigResponse.setMethod("airport_organization_bind");
        deviceConfigResponse.setGateway("8UUXN2800A00HX");

        MqttMessage message = new MqttMessage();
        message.setQos(1);
        message.setRetained(true);
        message.setPayload(JSONObject.toJSONString(deviceConfigResponse).getBytes());
        MqttClient mqttClient = new MqttClient("tcp://120.211.119.131:1883", "feisuan-mqtt-producer-client1", new MemoryPersistence());
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setUserName("feisuan");
        connOpts.setPassword("feisuan123456".toCharArray());
        mqttClient.connect(connOpts);

        mqttClient.publish("thing/product/" + "8UUXN2800A00HX" + "/requests_reply", message);
//        mqttClient.publish("thing/product/" + "1581F8HGX252500A0022" + "/requests", message);
    }


    public void send() throws MqttException {
//        AirportBindDevicesData data = new AirportBindDevicesData();
//        List<AirportBindDevicesData.BindDevice> list = new ArrayList<>();

        DeviceConfigResponse<AirportBindDevicesData.BindDevice> deviceConfigResponse = new DeviceConfigResponse<>();


        AirportBindDevicesData.BindDevice bindDevice = new AirportBindDevicesData.BindDevice();
        bindDevice.setBindingCode("iOfc2X");
        bindDevice.setCallsign("飞行员一号");
//        bindDevice.setSerialNumber("8UUXN2800A00HX");
        bindDevice.setModelKey("0-100-1");
        bindDevice.setOrganizationId("1");
        bindDevice.setSerialNumber("1581F8HGX252500A0022");
        deviceConfigResponse.setData(bindDevice);
        deviceConfigResponse.setTimestamp(System.currentTimeMillis());
        deviceConfigResponse.setMethod("airport_organization_bind");
        deviceConfigResponse.setBid(UUID.randomUUID().toString());
        deviceConfigResponse.setTid(UUID.randomUUID().toString());
        deviceConfigResponse.setGateway("8UUXN2800A00HX");

        MqttMessage message = new MqttMessage();
        message.setQos(1);
        message.setRetained(true);
        message.setPayload(JSONObject.toJSONString(deviceConfigResponse).getBytes());
        MqttClient mqttClient = new MqttClient("tcp://120.211.119.131:1883", "feisuan-mqtt-producer-client", new MemoryPersistence());
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setUserName("feisuan");
        connOpts.setPassword("feisuan123456".toCharArray());
        mqttClient.connect(connOpts);

        mqttClient.publish("thing/product/" + "8UUXN2800A00HX" + "/requests", message);

    }

}
